#include <iostream>
#include <vector>
#include "lib/list.h"
using namespace std;

ListNode* reverseListIteration(ListNode* phead) {
  ListNode* tail_node = nullptr;
  ListNode* cur_node = phead;
  ListNode* pre_node = nullptr;
  while (cur_node) {
    auto next_node = cur_node->next;
    if (next_node == nullptr)
      tail_node = cur_node;
    cur_node->next = pre_node;
    pre_node = cur_node;
    cur_node = next_node;
  }
  return tail_node;
}

void reverseListCore(ListNode* pre_node, ListNode* &cur_node, ListNode* next_node) {
  cur_node->next = pre_node;
  if (next_node == nullptr) {
    return;
  }

  pre_node = cur_node;
  cur_node = next_node;
  reverseListCore(pre_node, cur_node, cur_node->next);
}

ListNode* reverseListRecursive(ListNode* node) {
  reverseListCore(nullptr, node, node->next);
  return node;
}

int main() {
  vector<int> vec = {1, 2, 3, 4, 5, 6, 7};
  ListNode* phead = CreateList(vec);
  cout << "Input list: " << endl;
  PrintList(phead);

  cout << "Reverse list first time" << endl;
  ListNode* tail = reverseListIteration(phead);
  PrintList(tail);

  cout << "Reverse lit second time" << endl;
  ListNode* tail_recursive = reverseListRecursive(tail);
  PrintList(tail_recursive);

  DeleteList(tail_recursive);

  return 0;
}
